package com.lindroy.androidplayer.base

import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
import com.king.retrofit.retrofithelper.RetrofitHelper
import com.lindroy.androidplayer.AppViewModel
import com.lindroy.androidplayer.BuildConfig
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.constants.DOMIN_UPDATE
import com.lindroy.androidplayer.constants.LOG_TAG
import com.lindroy.androidplayer.constants.UPDATE_URL
import com.lindroy.androidplayer.extensions.EventViewModel
import com.lindroy.multistatelayout.MultiStateLayout
import com.orhanobut.logger.AndroidLogAdapter
import com.orhanobut.logger.Logger
import com.orhanobut.logger.PrettyFormatStrategy
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.MaterialHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.listener.DefaultRefreshHeaderCreator
import com.youngfeng.snake.Snake


/**
 * @author Lin
 * @date 2021/4/6
 * @function
 */

val appViewModel: AppViewModel by lazy { App.appViewModelInstance }
val eventViewModel: EventViewModel by lazy { App.eventViewModelInstance }

class App : Application(), ViewModelStoreOwner {
    private lateinit var appViewModelStore: ViewModelStore
    private var mFactory: ViewModelProvider.Factory? = null

    companion object {
        @SuppressLint("StaticFieldLeak")
        lateinit var context: Context
        lateinit var appViewModelInstance: AppViewModel
        lateinit var eventViewModelInstance: EventViewModel

        init {
            //设置全局的Header构建器
            SmartRefreshLayout.setDefaultRefreshHeaderCreator(DefaultRefreshHeaderCreator { context, layout ->
                layout.setPrimaryColorsId(R.color.white) //全局设置主题颜色
                MaterialHeader(context).apply {
                    setColorSchemeResources(R.color.primary_dark)
                }
                //.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
            })
            //设置全局的Footer构建器
            SmartRefreshLayout.setDefaultRefreshFooterCreator { context, layout -> //指定为经典Footer，默认是 BallPulseFooter
                ClassicsFooter(context).setDrawableSize(20F)
            }
        }
    }

    override fun onCreate() {
        super.onCreate()
        context = applicationContext
        appViewModelStore = ViewModelStore()
        appViewModelInstance = ViewModelProvider(this, getAppFactory())[AppViewModel::class.java]
        eventViewModelInstance = ViewModelProvider(this, getAppFactory())[EventViewModel::class.java]
        initLogger()
        initStateLayout()
        Snake.init(this)
        RetrofitHelper.getInstance().apply {
            putDomain(DOMIN_UPDATE, UPDATE_URL)
        }
    }

    private fun initStateLayout() {
        MultiStateLayout.init()
            .setEmptyView(R.layout.layout_empty)
            .setErrorView(R.layout.layout_network_error,clickViewIds = intArrayOf(R.id.btnRetry))
            .setLoadingView(R.layout.layout_loading)
            .setNoNetworkView(R.layout.layout_no_network,clickViewIds = intArrayOf(R.id.btnRetry))
    }

    private fun initLogger() {
        val formatStrategy = PrettyFormatStrategy.newBuilder().tag(LOG_TAG).build()
        Logger.addLogAdapter(object : AndroidLogAdapter(formatStrategy) {
            override fun isLoggable(priority: Int, tag: String?): Boolean {
                return BuildConfig.DEBUG
            }
        })
    }

    override fun getViewModelStore(): ViewModelStore = appViewModelStore

    private fun getAppFactory(): ViewModelProvider.Factory {
        if (mFactory == null) {
            mFactory = ViewModelProvider.AndroidViewModelFactory.getInstance(this)
        }
        return mFactory as ViewModelProvider.Factory
    }
}